package com.gxks.lhs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxks.lhs.entity.Category;
import com.gxks.lhs.mapper.CategoryMapper;
import com.gxks.lhs.service.ICategoryService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-05-18
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Override
    public boolean addCategory(Category category) {
        if (category.getName() != null) {
            return this.save(category);
        }
        return false;
    }

    @Override
    public boolean deleteCategory(Integer id) {
        if (id != null) {
            return this.removeById(id);
        }
        return false;
    }

    @Override
    public boolean updateCategory(Category category) {
        if (category.getId() != null) {
            return this.updateById(category);
        }
        return false;
    }

    @Override
    public Page<Category> findCategoryByName(String name, Integer current, Integer size) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(Category::getName, name);
        return this.page(new Page<>(current, size), queryWrapper);
    }

    @Override
    public Category findCategoryById(Integer id) {
        Category category = getById(id);
        return category;
    }

    @Override
    public Page<Category> findAllCategory(Integer current, Integer size) {
        return this.page(new Page<>(current, size));
    }

    @Override
    public Page<Category> findAllCategoryByUser(Integer current, Integer size) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(Category::getId, Category::getName, Category::getImg).orderByAsc(Category::getId);

        return this.page(new Page<>(current, size), queryWrapper);
    }

    @Override
    public List findCategoryName() {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        List<Category> categories = list();
        return categories;
    }

}
